package cn.tecnova.cd

import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{Dataset, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.rdd.EsSpark

/**
  * description:
  * 41 2420
  **/
object TecnovaPledge {
  Logger.getLogger("org").setLevel(Level.ERROR)

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val rowDF = sqLContext.read.jdbc("jdbc:mysql://www.slave4.com:3306/tec_pledge", "qichacha_pledge", ConfigHandler.props2)

    //导入写es相关的包
    import org.elasticsearch.spark.sql._

    val result: Dataset[TecnovaPledge] = rowDF.map(row => {
      TecnovaPledge(
        row.getAs[Int]("id"),
        row.getAs[String]("KeyNo"),
        row.getAs[String]("No"),
        row.getAs[String]("RegistNo"),
        row.getAs[String]("Pledgor"),
        row.getAs[String]("PledgorNo"),
        row.getAs[String]("Pledgee"),
        row.getAs[String]("PledgeeNo"),
        row.getAs[String]("PledgedAmount"),
        row.getAs[String]("RegDate"),
        row.getAs[String]("PublicDate"),
        row.getAs[String]("Status"),
        row.getAs[String]("Detail"),
        row.getAs[String]("CompanyId")

      )
    })
//    result.saveToEs("tecnova_pledge2" + "/company_pledge")
    EsSpark.saveToEs(result.rdd,"tecnova_pledge/company_pledge",Map("es.mapping.id" -> "id"))

    sc.stop()

  }

}

case class TecnovaPledge(

                             var id:Long,
                             var keyNo:String,
                             var no:String,
                             var registNo:String,
                             var pledgor:String,
                             var pledgorNo:String,
                             var pledgee:String,
                             var pledgeeNo:String,
                             var pledgedAmount:String,
                             var regDate:String,
                             var publicDate:String,
                             var status:String,
                             var detail:String,
                             var companyId:String

                           )
